﻿using System;
using System.Collections.Generic;
using System.Text;
using VectorMath;

namespace FluidSystem
{
    class BoundedVelocityGrid : BoundedGrid<Pnt3D>
    {

        //private Pnt3D[, ,] values;
        //private int xSize, ySize, zSize;

        public BoundedVelocityGrid(int x, int y, int z)
            : base(x, y, z)
        {

        }

        /// <summary>
        /// valid elements are 0 thru Size - 1, bounds are -1 and size
        /// </summary>
        public override Pnt3D this[int x, int y, int z]
        {

            get
            {
                double xr, yr, zr;



                if (x >= 0 && x < xSize && y >= 0 && y < ySize && z >= 0 && z < zSize)
                {

                    if (x == 0) values[x, y, z].x = 0;
                    if (y == 0) values[x, y, z].y = 0;
                    if (z == 0) values[x, y, z].z = 0;


                    return values[x, y, z];
                }

                //check for corneres
                bool xy = (x == -1 || x == xSize ) && (y == -1 || y == ySize );
                bool yz = (y == -1 || y == ySize ) && (z == -1 || z == zSize );
                bool xz = (x == -1 || x == xSize ) && (z == -1 || z == zSize );

                if (xy || yz || xz)
                {
                    return new Pnt3D(0, 0, 0);
                }

                if (x <= 0)
                {
                    xr = 0;
                }
                else if (x >= xSize)
                {
                    xr = 0;
                }
                else
                {
                    if (y == -1)
                    {
                        xr = values[x, 0, z].x;
                    }
                    else if (y == ySize)
                    {
                        xr = values[x, ySize - 1, z].x;
                    }
                    else
                    {
                        if (z == -1)
                        {
                            xr = values[x, y, 0].x;
                        }
                        else if (z == zSize)
                        {
                            xr = values[x, y, zSize - 1].x;
                        }
                        else
                        {
                            xr = values[x, y, z].x;
                        }
                    }
                }

                if (y <= 0)
                {
                    yr = 0;
                }
                else if (y >= ySize)
                {
                    yr = 0;
                }
                else
                {
                    if (x == -1)
                    {
                        yr = values[0, y, z].y;
                    }
                    else if (x == xSize)
                    {
                        yr = values[xSize - 1, y, z].y;
                    }
                    else
                    {
                        if (z == -1)
                        {
                            yr = values[x, y, 0].y;
                        }
                        else if (z == zSize)
                        {
                            yr = values[x, y, zSize - 1].y;
                        }
                        else
                        {
                            yr = values[x, y, z].y;
                        }
                    }
                }

                if (z <= 0)
                {
                    zr = 0;
                }
                else if (z >= zSize)
                {
                    zr = 0;
                }
                else
                {
                    if (x == -1)
                    {
                        zr = values[0, y, z].z;
                    }
                    else if (x == xSize)
                    {
                        zr = values[xSize - 1, y, z].z;
                    }
                    else
                    {
                        if (y == -1)
                        {
                            zr = values[x, 0, z].z;
                        }
                        else if (y == ySize)
                        {
                            zr = values[x, ySize - 1, z].z;
                        }
                        else
                        {
                            zr = values[x, y, z].z;
                        }
                    }
                }

                return new Pnt3D(xr, yr, zr);
            }

            set
            {
                if (x == -1)
                    throw new IndexOutOfRangeException("bound");
                else if (x == xSize)
                    throw new IndexOutOfRangeException("bound");
                else if (y == -1)
                    throw new IndexOutOfRangeException("bound");
                else if (y == ySize)
                    throw new IndexOutOfRangeException("bound");
                else if (z == -1)
                    throw new IndexOutOfRangeException("bound");
                else if (z == zSize)
                    throw new IndexOutOfRangeException("bound");
                else
                    values[x, y, z] = value;
            }
        }


        internal override void SetBorders(Pnt3D border)
        {
            throw new Exception("This doesn't work for velocities");
        }

    }
}
